
import 'package:flutter/widgets.dart';
import 'SliverListView.dart';

class SliverListViewFooter extends StatefulWidget {
  final SliverListViewFooterBuilder builder;
  final double height;
  final Alignment alignment;
  final EndReachedState state;
  final bool hasMore;
  final TextStyle textStyle;
  static const double _defaultHeight = 40;
  static const Alignment _defaultAlignment = Alignment.center;

  static Text defaultBuilder(BuildContext context, EndReachedState  state,{ bool hasMore, TextStyle textStyle }) {
    String str = hasMore ? "上拉加载" : "已经到底了";
    if(hasMore) {
      switch (state) {
        case EndReachedState.active:
          str = "正在加载";
          break;
        case EndReachedState.reached:
          str = "获取数据中...";
          break;
        default:
          break;
      }
    }
    return Text(str, style: textStyle);
  }
  const SliverListViewFooter({Key key, 
    this.builder = defaultBuilder,
    this.alignment = _defaultAlignment, 
    this.height = _defaultHeight, 
    this.state, 
    this.hasMore = false,
    this.textStyle
  }) : super(key: key);

  _SliverListViewFooterState createState() => _SliverListViewFooterState();
}

class _SliverListViewFooterState extends State<SliverListViewFooter> {
  @override
  Widget build(BuildContext context) {
    EndReachedState _state = widget.state != null ? widget.state : SliverListView.of(context).reachedState;
    return SliverToBoxAdapter(
      child: Container(
        height: widget.height,
        alignment: widget.alignment,
        child: widget.builder(context, _state, hasMore: widget.hasMore, textStyle: widget.textStyle),
      ),
    );
  }
}